package com.youdoneed.user.photo;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.base.pagination.Page;
import com.youdoneed.base.pagination.Paging;
import com.youdoneed.user.consts.UserConst;
import com.youdoneed.user.model.Photo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 照片请求控制器。
 *
 * @author Ewing
 * @date 2017.2.8
 */
@RestController
@RequestMapping("/photo")
@Api(description = "用户照片")
public class PhotoController {

    @Autowired
    private PhotoService photoService;

    /**
     * 用户新增照片。
     */
    @ApiOperation("用户新增照片")
    @RequestMapping(value = "/addPhoto", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage addPhoto(@RequestHeader(UserConst.TOKEN_KEY) String token, Photo photo) {
        String userId = JWTUtils.getTokenUserId(token);
        photo.setUserId(userId); // 只能给自己新增照片
        photoService.addPhoto(photo);
        return new ResultMessage();
    }

    /**
     * 用户新增多张照片。
     */
    @ApiOperation("用户新增多张照片")
    @RequestMapping(value = "/addPhotos", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage addPhotos(@RequestHeader(UserConst.TOKEN_KEY) String token, @RequestBody Photo[] photos) {
        String userId = JWTUtils.getTokenUserId(token);
        photoService.addPhotos(userId, photos);
        return new ResultMessage();
    }

    /**
     * 根据ID获取照片。
     */
    @ApiOperation("根据ID获取照片")
    @RequestMapping(value = "/getPhoto", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Photo> getPhoto(String photoId) {
        return new ResultMessage<>(photoService.getPhoto(photoId));
    }

    /**
     * 用户修改照片。
     */
    @ApiOperation("用户修改照片")
    @RequestMapping(value = "/updatePhoto", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage updatePhoto(@RequestHeader(UserConst.TOKEN_KEY) String token, Photo photo) {
        String userId = JWTUtils.getTokenUserId(token);
        photoService.updatePhoto(userId, photo);
        return new ResultMessage();
    }

    /**
     * 用户删除照片。
     */
    @ApiOperation("用户删除照片")
    @RequestMapping(value = "/deletePhoto", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage deletePhoto(@RequestHeader(UserConst.TOKEN_KEY) String token, String photoId) {
        String userId = JWTUtils.getTokenUserId(token);
        photoService.deletePhoto(userId, photoId);
        return new ResultMessage();
    }

    /**
     * 获取我的所有照片。
     */
    @ApiOperation("获取我的所有照片")
    @RequestMapping(value = "/myPhotos", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Page<Photo>> myPhotos(@RequestHeader(UserConst.TOKEN_KEY) String token, Paging paging) {
        String userId = JWTUtils.getTokenUserId(token);
        Page<Photo> photos = photoService.userPhotos(userId, paging);
        return new ResultMessage<>(photos);
    }

    /**
     * 获取用户的所有照片。
     */
    @ApiOperation("获取用户的所有照片")
    @RequestMapping(value = "/userPhotos", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Page<Photo>> userPhotos(String userId, Paging paging) {
        Page<Photo> photos = photoService.userPhotos(userId, paging);
        return new ResultMessage<>(photos);
    }

}
